package com.chen.exception;

import com.chen.response.Result;
import com.chen.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*使用aop去实现统一的异常的处理的,但是我使用的是注解版*/
/*这个注解能够监听所有的controller通知，只要出现异常立马被捕获*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ArithmeticException.class)
    public Result arithmeticException(ArithmeticException e){
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error(ResultCode.ARITHMETIC_EXCEPTION);
    }

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public Result arrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e){
        e.printStackTrace();
        //日志
        log.error(e.getMessage());
        return Result.error(ResultCode.ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION);
    }

    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerException(NullPointerException e){
        e.printStackTrace();
        log.error("空指针:"+e.getMessage()+"---------------->");
        return Result.error(ResultCode.NULL_POINTER_EXCEPTION);
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error(ResultCode.COMMON_FAIL);
    }
}